package com.lgcns.board;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.security.access.prepost.PreAuthorize;

import com.lgcns.commons.PagingHelper;

@Service
public class BoardService {

	@Autowired
	private BoardDao boardDao;
	private PagingHelper pagingHelper;
	
	public BoardService()
	{
		
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public ArrayList<Article> getArticleList(String boardCd, String searchWord, int start, int end)
	{
		Integer startRownum = start;
		Integer endRownum = end;
		
		HashMap<String, String> hashmap = new HashMap<String, String>();
		hashmap.put("boardCd", boardCd);
		hashmap.put("searchWord", searchWord);
		hashmap.put("start", startRownum.toString());
		hashmap.put("end", endRownum.toString());
		
		return boardDao.getArticleList(hashmap);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int getTotalRecord(String boardCd, String searchWord)
	{
		HashMap<String, String> hashmap = new HashMap<String, String>();
		hashmap.put("boardCd", boardCd);
		hashmap.put("searchWord", searchWord);
		
		return boardDao.getTotalRecord(hashmap);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int insert(Article article)
	{
		return boardDao.insert(article);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public AttachFile getAttachFile(int attachFileNo)
	{
		return boardDao.getAttachFile(attachFileNo);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public void insertAttachFile(AttachFile attachFile)
	{
		boardDao.insertAttachFile(attachFile);
	}
	
	@PreAuthorize("#article.email == principal.username or hasRole('ROLE_ADMIN')")
	public void update(Article article)
	{
		boardDao.update(article);
	}
	
	@PreAuthorize("#article.email == principal.username or hasRole('ROLE_ADMIN')")
	public void delete(Article article)
	{
		boardDao.delete(article.getArticleNo());
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public void increaseHit(int articleNo)
	{
		boardDao.increseHit(articleNo);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public Article getArticle(int articleNo)
	{
		return boardDao.getArticle(articleNo);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public Article getNextArticle(int articleNo, String boardCd, String searchWord)
	{
		HashMap<String, String> hashmap = new HashMap<String, String>();
		Integer no = articleNo;
		
		hashmap.put("articleNo", no.toString());
		hashmap.put("boardCd", boardCd);
		hashmap.put("searchWord", searchWord);
		
		return boardDao.getNextArticle(hashmap);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public Article getPrevArticle(int articleNo, String boardCd, String searchWord)
	{
		HashMap<String, String> hashmap = new HashMap<String, String>();
		Integer no = articleNo;
		
		hashmap.put("articleNo", no.toString());
		hashmap.put("boardCd", boardCd);
		hashmap.put("searchWord", searchWord);
		
		return boardDao.getPrevArticle(hashmap);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public ArrayList<AttachFile> getAttachFileList(int articleNo)
	{
		return boardDao.getAttachFileList(articleNo);
	}
	
	@PreAuthorize("#email == principal.username or hasRole('ROLE_ADMIN')")
	public void deleteFile(int attachFileNo)
	{
		boardDao.deleteFile(attachFileNo);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public String getBoardNm(String boardCd)
	{
		return boardDao.getBoardNm(boardCd);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public ArrayList<Board> getBoardList()
	{
		return boardDao.getBoardList();
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public void insertComment(Comment comment)
	{
		boardDao.insertComment(comment);
	}
	
	@PreAuthorize("#comment.email == principal.username or hasRole('ROLE_ADMIN')")
	public void updateComment(Comment comment)
	{
		boardDao.updateComment(comment);
	}
	
	@PreAuthorize("#comment.email == principal.username or hasRole('ROLE_ADMIN')")	
	public void deleteComment(Comment comment)
	{
		boardDao.deleteComment(comment.getCommentNo());
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public Comment getComment(int commentNo)
	{
		return boardDao.getComment(commentNo);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public ArrayList<Comment> getCommentList(int articleNo)
	{
		return boardDao.getCommentList(articleNo);
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int getListNo()
	{
		return pagingHelper.getListNo();
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int getPrevLink()
	{
		return pagingHelper.getPrevLink();
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int getFirstPage()
	{
		return pagingHelper.getFirstPage();
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int getLastPage()
	{
		return pagingHelper.getLastPage();
	}
	
	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int getNextLink()
	{
		return pagingHelper.getNextLink();
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public int[] getPageLinks()
	{
		return pagingHelper.getPageLinks();
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public PagingHelper getPagingHelper()
	{
		return pagingHelper;
	}

	@PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
	public void setPagingHelper(PagingHelper pagingHelper)
	{
		this.pagingHelper = pagingHelper;
	}
}
